/*
 * ring.c
 *
 *  Created on: Jun 2, 2022
 *      Author: 86153
 */
#include"ring.h"

/**
* @brief  RingBuff_Init
* @param  void
* @return void
* @note   初始化环形缓冲区
*/
void RingBuff_Init(RingBuff_t *ringBuff)
{
  //初始化相关信息
  ringBuff->Head = 0;
  ringBuff->Tail = 0;
  ringBuff->Lenght = 0;
}

/**
* @brief  Write_RingBuff
* @param  uint8_t data
* @return FLASE:环形缓冲区已满，写入失败;TRUE:写入成功
* @note   往环形缓冲区写入float类型的数据
*/
float Write_RingBuff(float data,RingBuff_t *ringBuff)
{
  if(ringBuff->Lenght >= RINGBUFF_LEN) //判断缓冲区是否已满
  {
    return RINGBUFF_ERR;
  }
  ringBuff->Ring_data[ringBuff->Tail]=data;
  ringBuff->Tail = (ringBuff->Tail+1)%RINGBUFF_LEN;//防止越界非法访问
  ringBuff->Lenght++;
  return RINGBUFF_OK;
}

/**
* @brief  Read_RingBuff
* @param  float *rData，用于保存读取的数据
* @return FLASE:环形缓冲区没有数据，读取失败;TRUE:读取成功
* @note   从环形缓冲区读取一个float类型的数据
*/
float Read_RingBuff(RingBuff_t *ringBuff)
{
	float rData;
  if(ringBuff->Lenght == 0)//判断非空
  {
    return RINGBUFF_ERR;
  }
  rData = ringBuff->Ring_data[ringBuff->Head];//先进先出FIFO，从缓冲区头出
  ringBuff->Head = (ringBuff->Head+1)%RINGBUFF_LEN;//防止越界非法访问
  ringBuff->Lenght--;
  return rData;
}

